package com.eastfair.home.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.eastfair.builder.ReportSpaceFeign;
import com.eastfair.builder.dto.ReportSpaceDTO;
import com.eastfair.builder.enumeration.ReportSpaceFinalDrawingAuditStatusEnum;
import com.eastfair.builder.enumeration.ReportSpaceReportSpaceStatusEnum;
import com.eastfair.builder.enumeration.ReportSpaceVenueAuditStatusEnum;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.home.dto.ReportSpaceAuditDTO;
import com.eastfair.home.entity.ReportSpaceAudit;
import com.eastfair.home.service.HomeReportSpaceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 搭建商报馆服务.
 *
 * @author dqq
 * @date 2022 /8/10
 */
@Service
@Slf4j
public class HomeReportSpaceServiceImpl implements HomeReportSpaceService {

    @Resource
    private ReportSpaceFeign reportSpaceFeign;

    @Resource(name = "threadPoolTaskExecutor")
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Override
    public boolean updateAuditStatus(ReportSpaceDTO reportSpaceDTO) {
        R<Boolean> result = reportSpaceFeign.updateAuditStatus(reportSpaceDTO);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean updateVenueAuditStatus(ReportSpaceDTO reportSpaceDTO) {
        log.info("updateVenueAuditStatus - 更新场馆审核状态, reportSpaceDTO={}", reportSpaceDTO);
        R<Boolean> result = reportSpaceFeign.updateVenueAuditStatus(reportSpaceDTO);
        log.info("updateVenueAuditStatus - 更新场馆审核状态, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean updateVenueAuditStatus(ReportSpaceAudit reportSpaceAudit) {
        ReportSpaceDTO reportSpaceDTO = new ReportSpaceDTO();
        reportSpaceDTO.setId(reportSpaceAudit.getReportSpaceId());

        ReportSpaceVenueAuditStatusEnum venueAuditStatus = ReportSpaceVenueAuditStatusEnum.get(reportSpaceAudit.getVenueAuditStatus());
        reportSpaceDTO.setVenueAuditStatus(venueAuditStatus);

        ReportSpaceFinalDrawingAuditStatusEnum finalDrawingAuditStatus = ReportSpaceFinalDrawingAuditStatusEnum.get(reportSpaceAudit.getFinalDrawingAuditStatus().getCode());
        reportSpaceDTO.setFinalDrawingAuditStatus(finalDrawingAuditStatus);

        ReportSpaceReportSpaceStatusEnum reportStatus = ReportSpaceReportSpaceStatusEnum.get(reportSpaceAudit.getReportSpaceStatus().getCode());
        reportSpaceDTO.setReportSpaceStatus(reportStatus);
        return updateVenueAuditStatus(reportSpaceDTO);
    }

    @Override
    public boolean sendDrawingAuditMsg(Long reportSpaceId) {
        log.info("sendDrawingAuditMsg - 发送图纸审核通过信息, reportSpaceId={}", reportSpaceId);
        R<Boolean> result = reportSpaceFeign.sendDrawingAuditMsg(reportSpaceId);
        log.info("sendDrawingAuditMsg - 发送图纸审核通过信息, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public void asyncSendDrawingAuditMsg(Long reportSpaceId) {
        threadPoolTaskExecutor.execute(() -> sendDrawingAuditMsg(reportSpaceId));
    }

    @Override
    public boolean sendDrawingAuditMsg(ReportSpaceAuditDTO reportSpaceAudit) {
        log.info("sendDrawingAuditMsg - 发送图纸审核通过信息, reportSpaceId={}", reportSpaceAudit);
        R<Boolean> result = reportSpaceFeign.sendDrawingAuditMsg(reportSpaceAudit);
        log.info("sendDrawingAuditMsg - 发送图纸审核通过信息, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public void asyncSendDrawingAuditMsg(ReportSpaceAudit reportSpaceAudit) {
        threadPoolTaskExecutor.execute(() -> {
            ReportSpaceAuditDTO reportSpaceAuditDTO = BeanUtil.toBean(reportSpaceAudit, ReportSpaceAuditDTO.class);
            sendDrawingAuditMsg(reportSpaceAuditDTO);
        });
    }
}